﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class Share_Insert_Update_ExhibitionControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["ExhibitionID"] != null)
        {
            fillForm(int.Parse(Request["ExhibitionID"].ToString()));
        }
        else
        {
            ResetForm();
            pn_Update.Visible = false;
            pn_Insert.Visible = true;
        }
    }
    public void fillForm(int exhID)
    {
        ExhibitionDAO exhibitionDAO = new ExhibitionDAO();
        ExhibitionInfo exh = new ExhibitionInfo();
        exh = exhibitionDAO.GetbyID(exhID);
        txtExhibitionName.Text = exh.ExhibitionName;
        txtTopic.Text = exh.Topic;
        txtEndDate.Text = exh.DateEnd.ToShortDateString();
        txtStartDate.Text = exh.DateStart.ToShortDateString();
        txtPlace.Text = exh.Place;
        txtOrgannizer.Text = exh.Organizer;
        pn_Insert.Visible = false;
    }
    public void ResetForm()
    {
        txtExhibitionName.Text = "";
        txtTopic.Text = "";
        txtEndDate.Text = "";
        txtStartDate.Text = "";
        txtPlace.Text = "";
        txtOrgannizer.Text = "";        
    }
    protected void btn_Reset_Click(object sender, EventArgs e)
    {
        ResetForm();
        
    }
    protected void btn_Cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Admin/MaintainExhibitions.aspx");
        ResetForm();
        pn_Update.Visible = false;
        pn_Insert.Visible = false;
    }
    protected void btn_Insert_Click(object sender, EventArgs e)
    {
        ExhibitionInfo exhInfo = new ExhibitionInfo();
        ExhibitionDAO exhDAO = new ExhibitionDAO();
        exhInfo.ExhibitionName = txtExhibitionName.Text;
        exhInfo.Topic = txtTopic.Text;
        exhInfo.DateStart = DateTime.Parse(txtStartDate.Text);
        exhInfo.DateEnd = DateTime.Parse(txtEndDate.Text);
        exhInfo.Place = txtPlace.Text;
        exhInfo.Organizer = txtOrgannizer.Text;

        if (exhDAO.InsertExhibition(exhInfo) > 0)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Insert Successfull!');", true);
            Response.Redirect("MaintainExhibitions.aspx");
            ResetForm();
            
        }
        else
        {
            lblResult.Text = "Insert Failed";
        }
    }
    protected void btn_Update_Click(object sender, EventArgs e)
    {
        ExhibitionInfo exhInfo = new ExhibitionInfo();
        ExhibitionDAO exhDAO = new ExhibitionDAO();
        int exhID = int.Parse(Request["ExhibitionID"].ToString());
        exhInfo.ExhibitionID = exhID;
        exhInfo.ExhibitionName = txtExhibitionName.Text;
        exhInfo.Topic = txtTopic.Text;
        exhInfo.DateStart = DateTime.Parse(txtStartDate.Text);
        exhInfo.DateEnd = DateTime.Parse(txtEndDate.Text);
        exhInfo.Place = txtPlace.Text;
        exhInfo.Organizer = txtOrgannizer.Text;

        

        if (exhDAO.UpdateExhibition(exhInfo) > 0)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Update Successfull!');", true);
            ResetForm();
            Response.Redirect("MaintainExhibitions.aspx");

            pn_Update.Visible = true;
            
        }
        else
            lblResult.Text = "Update Failed";

    }
}